use crate::database::properties::property;
use napi_ohos::Result;
use napi_derive_ohos::napi;

#[napi]
pub async fn save_property(k: String, v: String) -> Result<()> {
    property::save_property(k, v).await.map_err(crate::map_anyhow)
}

#[napi]
pub async fn load_property(k: String) -> Result<String> {
    property::load_property(k).await.map_err(crate::map_anyhow)
}

#[napi]
pub async fn load_int_property(k: String) -> Result<i64> {
    let v = property::load_property(k).await.map_err(crate::map_anyhow)?;
    if v.is_empty() {
        return Ok(0);
    }
    Ok(v.parse::<i64>().map_err(crate::into_anyhow).map_err(crate::map_anyhow)?)
}

#[napi]
pub async fn save_int_property(k: String, v: i64) -> Result<()> {
    save_property(k, v.to_string()).await.map_err(crate::map_anyhow)
}

#[napi]
pub async fn load_bool_property(k: String) -> Result<bool> {
    let v = property::load_property(k).await.map_err(crate::map_anyhow)?;
    if v.is_empty() {
        return Ok(false);
    }
    Ok(v == "true")
}

#[napi]
pub async fn save_bool_property(k: String, v: bool) -> Result<()> {
    save_property(k, v.to_string()).await.map_err(crate::map_anyhow)
}
